home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / formats / dxf / dxf_exmp.c next >
Encoding:
C/C++ Source or Header  |  1980-02-06  |  14.1 KB  |  498 lines

  1. From thinman@netcom.com Thu Jul 11 21:52:09 1991
  2. X-Mailer: Mail User's Shell (7.2.3 5/22/91)
  3. To: koziol@ncsa.uiuc.edu (Quincey Koziol)
  4. Subject: DXF C source
  5.  
  6. Here is the C source code for dxf2dkb, which read DXF files and
  7. spits out an input object for the DKB ray-tracer.  Code is
  8. always handy...
  9.  
  10. /* AutoCAD DXF file to DKB Data File Converter */
  11. /* Version 1.0 By Aaron A. Collins.  Written 8/13/90 */
  12. /* This program is released to the public domain. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #define BUFSIZE    2048
  19. #define FABS(x) ((x<0.0)?-x:x)
  20.  
  21. int getline (void);
  22. void writeobj (void);
  23. void finishobj (int color);
  24. void lookupcolor (int color, float *red, float *green, float *blue);
  25. int checkdegen(int a, int b, int c);
  26.  
  27. int groupcode;
  28. char inname[80], outname[80];
  29. char linbuf[BUFSIZE];
  30. FILE *infile, *outfile;
  31. long primitives = 0L, degenerates = 0L;
  32. char curobj[80];
  33. int curcolor;
  34. float curthick;
  35. float xcoords[10];
  36. float ycoords[10];
  37. float zcoords[10];
  38. float floats[10];
  39. float angles[10];
  40. int ints[10];
  41. float max_x, max_y, max_z, min_x, min_y, min_z;
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     char *index;
  48.  
  49.     printf("\n\nAutoCad DXF to DKB .Data file Translator\n");
  50.     printf("Version 1.0 Written By Aaron A. Collins, 8/13/90\n");
  51.     printf("This program is released to the public domain.\n\n");
  52.     if (argc < 2 || argc > 3)
  53.     {
  54.         printf("Usage:  %s infile[.DXF] [outfile[.DAT]]\n", argv[0]);
  55.         exit(1);
  56.     }
  57.     strcpy(inname, argv[1]);    /* make copy we can mess with */
  58.     if (!strchr(inname, '.'))    /* no dot present in filename? */
  59.         strcat(inname, ".DXF");
  60.     if (!(infile = fopen(inname, "r")))
  61.     {
  62.         printf("Cannot open input file %s!\n", inname);
  63.         exit(1);
  64.     }
  65.     if (argc < 3)            /* i.e. no 2nd name supplied... */
  66.     {
  67.         strcpy(outname, inname);
  68.         index = strchr(outname, '.');    /* find the dot */
  69.         strcpy(index, ".DAT");        /* make new ext. .DAT... */
  70.     }
  71.     else
  72.     {
  73.         strcpy(outname, argv[2]);
  74.         if (!strchr(inname, '.'))    /* no dot in filename? */
  75.             strcat(inname, ".DAT");    /* add .DAT extension */
  76.     }
  77.     if (!(outfile = fopen(outname, "w")))
  78.     {
  79.         printf("Cannot create output file %s!\n", outname);
  80.         fclose(infile);
  81.         exit(1);
  82.     }
  83.  
  84.     printf("\nPlease wait; Processing...");
  85.     
  86.     fprintf(outfile, "{ %s: Converted from AutoCad DXF File: %s }\n\n", outname, inname);
  87.     fprintf(outfile, "INCLUDE \"basicshapes.data\"\n\n");
  88.     fprintf(outfile, "{ Put a VIEW_POINT here... }\n\n");
  89.     fprintf(outfile, "{ Put a LIGHT_SOURCE here... }\n\n");
  90.     fprintf(outfile, "COMPOSITE\n");
  91.  
  92.     curobj[0] = '\0';    /* not working on any object currently */
  93.     curcolor = 7;        /* and it also doesn't have a color yet... */
  94.     max_x = max_y = max_z = -10000000.0;    /* init bounding limits */
  95.     min_x = min_y = min_z =  10000000.0;
  96.  
  97. find:    while (!feof(infile))    /* run file up to the "ENTITIES" section */
  98.     {
  99.         if (getline())        /* get a group code and a line */
  100.             goto stopit;
  101.         if (groupcode == 0)    /* file section mark */
  102.         {
  103.             if (strstr(linbuf, "EOF"))
  104.                 goto stopit;
  105.             if (strstr(linbuf, "SECTION"))
  106.             {
  107.                 if (getline())
  108.                     goto stopit;
  109.                 if (groupcode != 2)
  110.                     continue;
  111.                 if (strstr(linbuf, "ENTITIES"))
  112.                     break;
  113.             }
  114.         }
  115.     }
  116.     while (!feof(infile))        /* scan ENTITIES section */
  117.     {
  118.         if (getline())        /* get a group code and a line */
  119.             break;
  120.         if (groupcode < 10)    /* cardinal group codes */
  121.         {
  122.             switch(groupcode)
  123.             {
  124.                 case 0: /* start of entity, table, file sep */
  125.                     if (strstr(linbuf, "EOF"))
  126.                     {
  127.                         writeobj(); /* dump object */
  128.                         goto stopit;
  129.                     }
  130.                     if (strstr(linbuf, "ENDSEC"))
  131.                     {
  132.                         writeobj(); /* dump object */
  133.                         goto find;
  134.                     }
  135.                     writeobj();    /* dump old object */
  136.                     curobj[0] = '\0'; /* reset object */
  137.                     curcolor = 7;
  138.                     strcpy(curobj, linbuf);    /* get new */
  139.                     break;
  140.                 case 1:    /* primary text value for entity (?)*/
  141.                     break;
  142.                 case 2: /* block name, attribute tag, etc */
  143.                 case 3:    /* other names */
  144.                 case 4:
  145.                     break;
  146.                 case 5:    /* entity handle (hex string) */
  147.                     break;
  148.                 case 6: /* line type name */
  149.                     break;
  150.                 case 7: /* text style name */
  151.                     break;
  152.                 case 8: /* layer name */
  153.                     break;
  154.                 case 9: /* variable name ID (only in header)*/
  155.                     break;
  156.             }
  157.         }
  158.         else if (groupcode >= 10 && groupcode < 19) /* Some X coord */
  159.         {
  160.             sscanf(linbuf, "%f", &(xcoords[groupcode-10]));
  161.             if (xcoords[groupcode-10] > max_x)
  162.                 max_x = xcoords[groupcode-10];
  163.             if (xcoords[groupcode-10] < min_x)
  164.                 min_x = xcoords[groupcode-10];
  165.         }
  166.         else if (groupcode >= 20 && groupcode < 29) /* Some Y coord */
  167.         {
  168.             sscanf(linbuf, "%f", &(ycoords[groupcode-20]));
  169.             if (ycoords[groupcode-20] > max_y)
  170.                 max_y = ycoords[groupcode-20];
  171.             if (ycoords[groupcode-20] < min_y)
  172.                 min_y = ycoords[groupcode-20];
  173.         }
  174.         else if (groupcode >= 30 && groupcode < 38) /* Some Z coord */
  175.         {
  176.             sscanf(linbuf, "%f", &(zcoords[groupcode-30]));
  177.             if (zcoords[groupcode-30] > max_z)
  178.                 max_z = zcoords[groupcode-30];
  179.             if (zcoords[groupcode-30] < min_z)
  180.                 min_z = zcoords[groupcode-30];
  181.         }
  182.         else if (groupcode == 38) /* entity elevation if nonzero */
  183.         {
  184.         }
  185.         else if (groupcode == 39) /* entity thickness if nonzero */
  186.         {
  187.         }
  188.         else if (groupcode >= 40 && groupcode < 49) /* misc floats */
  189.         {
  190.             sscanf(linbuf, "%f", &(floats[groupcode-40]));
  191.         }
  192.         else if (groupcode == 49) /* repeated value groups */
  193.         {
  194.         }
  195.         else if (groupcode >= 50 && groupcode < 59) /* misc angles */
  196.         {
  197.             sscanf(linbuf, "%f", &(angles[groupcode-50]));
  198.         }
  199.         else if (groupcode == 62) /* Color number */
  200.         {
  201.             sscanf(linbuf, "%6d", &curcolor);
  202.         }
  203.         else if (groupcode == 66) /* "entities follow" flag */
  204.         {
  205.         }
  206.         else if (groupcode >= 70 && groupcode < 79) /* misc ints */
  207.         {
  208.             sscanf(linbuf, "%f", &(ints[groupcode-70]));
  209.         }
  210.         else if (groupcode == 210 || groupcode == 220 || groupcode == 230)
  211.         {    /* X, Y, Z components of extrusion direction */
  212.         }
  213.     }
  214. stopit: fprintf(outfile, "  BOUNDED_BY\n    INTERSECTION\n");
  215.     fprintf(outfile, "      PLANE <1.0  0.0  0.0> %1.04f END_PLANE\n", FABS(max_x) * 1.01);
  216.     fprintf(outfile, "      PLANE <-1.0 0.0  0.0> %1.04f END_PLANE\n", FABS(min_x) * 1.01);
  217.     fprintf(outfile, "      PLANE <0.0  1.0  0.0> %1.04f END_PLANE\n", FABS(max_y) * 1.01);
  218.     fprintf(outfile, "      PLANE <0.0 -1.0  0.0> %1.04f END_PLANE\n", FABS(min_y) * 1.01);
  219.     fprintf(outfile, "      PLANE <0.0  0.0  1.0> %1.04f END_PLANE\n", FABS(max_z) * 1.01);
  220.     fprintf(outfile, "      PLANE <0.0  0.0 -1.0> %1.04f END_PLANE\n", FABS(min_z) * 1.01);
  221.     fprintf(outfile, "    END_INTERSECTION\n  END_BOUND\n   \nEND_COMPOSITE\n");
  222.     fclose(infile);
  223.     fflush(outfile);
  224.     fclose(outfile);
  225.     printf("Finished.\n\nTotal objects written to output file: %ld\n\n", primitives);
  226.     printf("Total degenerate triangles removed from scene: %ld\n\n", degenerates);
  227.     printf ("X bounding values range from %f to %f\n", min_x, max_x);
  228.     printf ("Y bounding values range from %f to %f\n", min_y, max_y);
  229.     printf ("Z bounding values range from %f to %f\n", min_z, max_z);
  230.     exit(0);
  231. }
  232.  
  233.  
  234. int getline()        /* read a group code and the next line from infile */
  235. {
  236.     fgets(linbuf, BUFSIZE, infile);    /* get a line from .DXF */
  237.     if (feof(infile))
  238.         return(1);
  239.     sscanf(linbuf, "%3d", &groupcode);  /* scan out group code */
  240.     fgets(linbuf, BUFSIZE, infile);    /* get a line from .DXF */
  241.     if (feof(infile))
  242.         return(1);
  243.     return(0);
  244. }
  245.  
  246. void writeobj()    /* dump out current object we should have all info on */
  247. {
  248.     if (strstr(curobj, "LINE"))        /* a VERY skinny triangle! */
  249.     {
  250.         if (xcoords[0] == xcoords[1] && ycoords[0] == ycoords[1] && zcoords[0] == zcoords[1])
  251.         {
  252.             degenerates++;
  253.             return;
  254.         }
  255.  
  256.         fprintf(outfile, "OBJECT\n");
  257.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[1], ycoords[1], zcoords[1], xcoords[1]+0.01, ycoords[1], zcoords[1]);
  258.         finishobj(curcolor);
  259.  
  260.         return;
  261.     }
  262.     else if (strstr(curobj, "POINT"))    /* an itty, bitty sphere! */
  263.     {
  264.         fprintf(outfile, "OBJECT\n");
  265.         fprintf(outfile, "  SPHERE <%1.06f %1.06f %1.06f> 0.1 END_SPHERE\n", xcoords[0], ycoords[0], zcoords[0]);
  266.         finishobj(curcolor);
  267.         return;
  268.     }
  269.     else if (strstr(curobj, "CIRCLE"))    /* a VERY short cylinder! */
  270.     {
  271.         fprintf(outfile, "OBJECT\n");
  272.         fprintf(outfile, "  INTERSECTION\n");
  273.         fprintf(outfile, "    QUADRIC Cylinder_Z SCALE <%1.06f %1.06f %1.06f> END_QUADRIC\n", floats[0], floats[0], floats[0]);
  274.         fprintf(outfile, "    PLANE <0.0 0.0 1.0>  0.1 END_PLANE\n");
  275.         fprintf(outfile, "    PLANE <0.0 0.0 -1.0> 0.1 END_PLANE\n");
  276.         fprintf(outfile, "  END_INTERSECTION\n");
  277.         fprintf(outfile, "  TRANSLATE <%1.06f %1.06f %1.06f>\n", xcoords[0], ycoords[0], zcoords[0]);
  278.         finishobj(curcolor);
  279.         return;
  280.     }
  281.     else if (strstr(curobj, "ARC"))        /* not implemented for now */
  282.     {
  283.         return;
  284.     }
  285.     else if (strstr(curobj, "TRACE"))    /* 2 back-to-back triangles */
  286.     {
  287.         if (checkdegen(0, 1, 2))
  288.         {
  289.             degenerates++;
  290.             return;
  291.         }
  292.  
  293.         fprintf(outfile, "OBJECT\n");
  294.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[1], ycoords[1], zcoords[1], xcoords[2], ycoords[2], zcoords[2]);
  295.         finishobj(curcolor);
  296.  
  297.         if (checkdegen(0, 3, 2))
  298.         {
  299.             degenerates++;
  300.             return;
  301.         }
  302.  
  303.         fprintf(outfile, "OBJECT\n");
  304.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[3], ycoords[3], zcoords[3], xcoords[2], ycoords[2], zcoords[2]);
  305.         finishobj(curcolor);
  306.  
  307.         return;
  308.     }
  309.     else if (strstr(curobj, "SOLID"))    /* 1 or 2 triangles */
  310.     {
  311.         if (checkdegen(0, 1, 2))
  312.         {
  313.             degenerates++;
  314.             return;
  315.         }
  316.  
  317.         fprintf(outfile, "OBJECT\n");
  318.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[1], ycoords[1], zcoords[1], xcoords[2], ycoords[2], zcoords[2]);
  319.         finishobj(curcolor);
  320.  
  321.         if (xcoords[2] == xcoords[3] && ycoords[2] == ycoords[3] && zcoords[2] == zcoords[3])
  322.             return;    /* one triangle was enough... */
  323.  
  324.         if (checkdegen(0, 3, 2))
  325.         {
  326.             degenerates++;
  327.             return;
  328.         }
  329.  
  330.         fprintf(outfile, "OBJECT\n");
  331.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[3], ycoords[3], zcoords[3], xcoords[2], ycoords[2], zcoords[2]);
  332.         finishobj(curcolor);
  333.         return;
  334.     }
  335.     else if (strstr(curobj, "TEXT"))    /* not implemented for now */
  336.     {
  337.         return;
  338.     }
  339.     else if (strstr(curobj, "SHAPE"))    /* these look very hard */
  340.     {
  341.         return;
  342.     }
  343.     else if (strstr(curobj, "BLOCK"))    /* these look very hard */
  344.     {
  345.         return;
  346.     }
  347.     else if (strstr(curobj, "ENDBLK"))    /* these look very hard */
  348.     {
  349.         return;
  350.     }
  351.     else if (strstr(curobj, "INSERT"))    /* these look very hard */
  352.     {
  353.         return;
  354.     }
  355.     else if (strstr(curobj, "ATTDEF"))    /* not implemented for now */
  356.     {
  357.         return;
  358.     }
  359.     else if (strstr(curobj, "ATTRIB"))    /* not implemented for now */
  360.     {
  361.         return;
  362.     }
  363.     else if (strstr(curobj, "POLYLINE"))    /* these look fairly hard */
  364.     {
  365.         return;
  366.     }
  367.     else if (strstr(curobj, "VERTEX"))    /* these look fairly hard */
  368.     {
  369.         return;
  370.     }
  371.     else if (strstr(curobj, "SEQEND"))    /* these look fairly hard */
  372.     {
  373.         return;
  374.     }
  375.     else if (strstr(curobj, "3DLINE"))    /* a VERY skinny triangle! */
  376.     {
  377.         if (xcoords[0] == xcoords[1] && ycoords[0] == ycoords[1] && zcoords[0] == zcoords[1])
  378.         {
  379.             degenerates++;
  380.             return;
  381.         }
  382.  
  383.         fprintf(outfile, "OBJECT\n");
  384.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[1], ycoords[1], zcoords[1], xcoords[1]+0.1, ycoords[1], zcoords[1]);
  385.         finishobj(curcolor);
  386.  
  387.         return;
  388.     }
  389.     else if (strstr(curobj, "3DFACE"))    /* 1 or 2 triangles */
  390.     {
  391.         if (checkdegen(0, 1, 2))
  392.         {
  393.             degenerates++;
  394.             return;
  395.         }
  396.  
  397.         fprintf(outfile, "OBJECT\n");
  398.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[1], ycoords[1], zcoords[1], xcoords[2], ycoords[2], zcoords[2]);
  399.         finishobj(curcolor);
  400.  
  401.         if (xcoords[2] == xcoords[3] && ycoords[2] == ycoords[3] && zcoords[2] == zcoords[3])
  402.             return;    /* one triangle was enough... */
  403.  
  404.         if (checkdegen(0, 3, 2))
  405.         {
  406.             degenerates++;
  407.             return;
  408.         }
  409.  
  410.         fprintf(outfile, "OBJECT\n");
  411.         fprintf(outfile, "  TRIANGLE <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> <%1.06f %1.06f %1.06f> END_TRIANGLE\n", xcoords[0], ycoords[0], zcoords[0], xcoords[3], ycoords[3], zcoords[3], xcoords[2], ycoords[2], zcoords[2]);
  412.         finishobj(curcolor);
  413.         return;
  414.     }
  415.     else if (strstr(curobj, "DIMENSION"))    /* not implemented for now */
  416.     {
  417.         return;
  418.     }
  419.     return;    /* no current object defined... */
  420. }
  421.  
  422. void finishobj(color)    /* conclude a DKB-style object definition */
  423. int color;
  424. {
  425.     float red, green, blue;
  426.  
  427.     lookupcolor(color, &red, &green, &blue);
  428.     fprintf(outfile, "\n  COLOR RED %1.06f GREEN %1.06f BLUE %1.06f\n", red, green, blue);
  429.     fprintf(outfile, "  TEXTURE Dull END_TEXTURE\n");  /* default surf. */
  430.     fprintf(outfile, "END_OBJECT\n\n");
  431.     printf(".");        /* activity echo (happy dots) */
  432.     primitives++;        /* count another output file primitive */
  433. }
  434.  
  435. void lookupcolor(color, red, green, blue) /* basic AutoCAD 9-color pallette */
  436. int color;
  437. float *red, *green, *blue;
  438. {
  439.     switch (color)
  440.     {
  441.         case 0:    /* black */
  442.             *red = *green = *blue = 0.0;
  443.             break;
  444.         case 1: /* red */
  445.             *red = 1.0;
  446.             *blue = *green = 0.0;
  447.             break;
  448.         case 2: /* yellow */
  449.             *red = *green = 1.0;
  450.             *blue = 0.0;
  451.             break;
  452.         case 3:    /* green */
  453.             *green = 1.0;
  454.             *red = *blue = 0.0;
  455.             break;
  456.         case 4: /* cyan */
  457.             *blue = *green = 1.0;
  458.             *red = 0.0;
  459.             break;
  460.         case 5: /* blue */
  461.             *blue = 1.0;
  462.             *red = *green = 0.0;
  463.             break;
  464.         case 6: /* magenta */
  465.             *blue = *red = 1.0;
  466.             *green = 0.0;
  467.             break;
  468.         case 8:    /* dk. grey */
  469.             *red = *green = *blue = 0.5;
  470.             break;
  471.         case 9: /* lt. grey */
  472.             *red = *green = *blue = 0.75;
  473.             break;
  474.         case 7: /* white */
  475.         default: /* make anything else white (?) */
  476.             *red = *green = *blue = 1.0;
  477.     }
  478.     return;
  479. }
  480.  
  481. int checkdegen(a, b, c)        /* check for degenerate triangle structure */
  482. int a, b, c;
  483. {
  484.     if (
  485.     (xcoords[a] == xcoords[b] &&
  486.      ycoords[a] == ycoords[b] &&
  487.      zcoords[a] == zcoords[b]) || 
  488.     (xcoords[b] == xcoords[c] &&
  489.      ycoords[b] == ycoords[c] &&
  490.      zcoords[b] == zcoords[c]) || 
  491.     (xcoords[a] == xcoords[c] &&
  492.      ycoords[a] == ycoords[c] &&
  493.      zcoords[a] == zcoords[c]))
  494.         return(1);
  495.     return(0);
  496. }
  497.  
  498.